// import md5 from 'js-md5'
import { getMd5Sign } from '../../utils/util'

const request = require('../config/index')

/**
 * 行驶证识别
 * @param fileUrl
 * @param img_type
 */
export const getXSZ = (fileUrl: string, img_type = 'FRONT') => {
    let body = {
        API_Model: 'Public', //API模块名
        API_Name: 'Ocr_Car', //API方法名
        Data: {
            img_url: fileUrl, //图片完整地址,即通用文件上传接口返回的相对地址加上API地址
            img_type //行驶证照片类型 FRONT 正页 BACK 副页
        }
    }

    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 驾驶证识别
 * @param fileUrl
 */
export const getJSZ = (fileUrl: string) => {
    let body = {
        API_Model: 'Public', //API模块名
        API_Name: 'Ocr_Driver', //API方法名
        Data: {
            img_url: fileUrl //图片完整地址,即通用文件上传接口返回的相对地址加上API地址
        }
    }

    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取用户信息
 */
export const getUserInfo = () => {
    const openid = wx.getStorageSync('openid')

    let body = {
        API_Model: 'Public', //API模块名
        API_Name: 'Get_User_Info', //API方法名
        Data: {
            openid //用户的唯一ID
        } //规划路线信息返回
    }

    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 完善用户信息
 * @param userInfo
 */
export const setUserInfo = (userInfo: any) => {
    const openid = wx.getStorageSync('openid')
    let body = {
        API_Model: 'Public', //API模块名
        API_Name: 'Perfect_User', //API接口名
        Data: {
            openid, //用户的唯一ID
            ...userInfo,
            user_address: '' //用户的地址
        } //API交互的参数
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取图片上的身份证号码
 * @param fileUrl
 */
export const getIDCard = (fileUrl: string) => {
    let body = {
        API_Model: 'Public', //API模块名
        API_Name: 'Ocr_IDCard', //API方法名
        Data: {
            img_url: fileUrl //图片完整地址,即通用文件上传接口返回的相对地址加上API地址
        }
    }

    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * 获取token
 * @param openid
 */
export const getUserToken = (openid: string, type = 1) => {
    let body = {
        API_Model: 'Public',
        API_Name: 'WxCodeToToken',
        Data: {
            openid,
            user_type: type //用户类型 1普通用户 2司机 3管理员
        }
    }

    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

/**
 * CODE换取用户唯一ID
 */
export const codeExchangeID = (code: string) => {
    let body = {
        API_Model: 'Public',
        API_Name: 'WxCodeToUser',
        Data: {
            code: code
        }
    }
    const md5Res = getMd5Sign(body)

    return new Promise((resolve, reject) => {
        request
            .post(`/api/doAPI`, body, {
                sign: md5Res
            })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}
